#include "ParamMicroscopyView.h"
#include "ui_ParamMicroscopyForm.h"
#include <QScrollBar>
#include <QDebug>
#include "Auxiliary/View/MessageBoxDialog.h"
#include "GlobalApi/sqlitedb.h"

ParamMicroscopyView::ParamMicroscopyView(QWidget *parent) : QWidget(parent),
  ui(new Ui::ParamMicroscopy)
{
   ui->setupUi(this);
   this->setWindowFlags(Qt::FramelessWindowHint);

   InitTable();
}

ParamMicroscopyView::~ParamMicroscopyView()
{
   delete ui;
}

void ParamMicroscopyView::InitTable()
{
    pMicroscopyTbv = new TableWgt;
    ui->gLayout_Microscopy->addWidget(pMicroscopyTbv);
    pMicroscopyTbv->setFixedSize(500,360);
    pMicroscopyTbv->sethHeaderHeight(30);
    pMicroscopyTbv->setvHeaderWidth(50);
    pMicroscopyTbv->showWithVerControlBtn(true);
    pMicroscopyTbv->showWithHorControlBtn(false);
    pMicroscopyTbv->setTableEditTri(true);

    pModel = new MySqlTableModel(this, SqliteDb::getInstance()->getDB());
    pModel->setTable("paramMicroscopy");
    pModel->select();
    pModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
    pMicroscopyTbv->getTableView()->setModel(pModel);
    pModel->setHeaderData(0, Qt::Horizontal, tr("镜检参数名称"));


    //声明一个委托
    delegate = new LineEditDelegate(this);

    QString conditions = "^[^,]{1,30}$";
    delegate->getlineeditconditions(conditions);
    pMicroscopyTbv->getTableView()->setItemDelegate(delegate);
}

int ParamMicroscopyView::microscopy_compare_param_to_bak()
{
    int i = 0,j = 0,ret = 0,rowcount;
    QString log_temp[3]={"","",""};

    rowcount = pModel->rowCount();
    for( i = 0,j = 0; i < paramMicroDataBak_now.count(); i++ )
    {
        if(paramMicroDataBak_now.at(i)=="")
        continue;
        if(j<rowcount)
        {
            paramMicroDataBak_now.replace(i, pModel->data(pModel->index(j, 0)).toString().trimmed());
            j++;
        }
    }

    for(i=0;i<paramMicroDataBak_last.count();i++)
    {
        if(paramMicroDataBak_last.at(i)!="" && paramMicroDataBak_now.at(i) == "")
        {
            log_temp[0]=log_temp[0]+"("+paramMicroDataBak_last.at(i)+")\n";
            ret = 1;
        }
        else if(paramMicroDataBak_last.at(i)=="" && paramMicroDataBak_now.at(i) != "")
        {
            log_temp[1]=log_temp[1]+"("+paramMicroDataBak_now.at(i)+")\n";
            ret = 1;
        }
        else if(paramMicroDataBak_last.at(i) != paramMicroDataBak_now.at(i))
        {
            log_temp[2]=log_temp[2]+"("+paramMicroDataBak_last.at(i)+tr("→")+paramMicroDataBak_now.at(i)+")\n";
            ret = 1;
        }
    }

    if(log_temp[0]!="")
    logtemp.append(tr("修改镜检参数设置为：删除\n")+log_temp[0]);
    if(log_temp[1]!="")
    logtemp.append(tr("修改镜检参数设置为：新增\n")+log_temp[1]);
    if(log_temp[2]!="")
    logtemp.append(tr("修改镜检参数设置为：修改\n")+log_temp[2]);

    return ret;
}

void ParamMicroscopyView::close_softKeyboard_handle()
{

}

int ParamMicroscopyView::micros_judge_name_if_repeat()
{
    int rowcount = 0;
    paramMicroDataBak.clear();

    rowcount = pModel->rowCount();


    for( int i = 0; i < rowcount; i++ )
    {
        paramMicroDataBak.append(pModel->data(pModel->index(i, 0)).toString().trimmed());
    }
    for( int i = 0; i < rowcount; i++ )
         for( int j = i+1; j < rowcount; j++ )
             if(paramMicroDataBak[i]==paramMicroDataBak[j])
             {
//                 hmsgBox->setTipInfo(tr("名称重复，退出将无法保存，是否退出？"));
//                 hmsgBox->setBottonText("OK", tr("是"));
//                 hmsgBox->setBottonText("Cancel", tr("否"));
//                 hmsgBox->showKMessageBox();


//                 if( hmsgBox->getStates() == KMessageBox::OK)
//                 {
//                     return 2;  //退出该界面不保存

//                 }else if( hmsgBox->getStates() == KMessageBox::Cancel)
//                 {
//                     return 0;
//                 }

             }

    return 1;  //正常
}

bool ParamMicroscopyView::modules_change_save()
{
    int i = 0;

    int rowCount = paramMicroDataBak.count();
    for( i = 0; i < rowCount; i++ )
   {
        if(paramMicroDataBak[i] == tr(""))
        {
//            msgBox->setTipInfo(tr("镜检参数名称为空，退出将无法保存，是否退出？"));
//            msgBox->setBottonText("OK", tr("是"));
//            msgBox->setBottonText("Cancel", tr("否"));
//            msgBox->showBotton("Cancel");
//            msgBox->showKMessageBox();

//            if( msgBox->getStates() == KMessageBox::OK)
//            {

//                return 1;
//            }
//            else
//            {
//                return 0;
//            }
        }
    }

    if( 0 == microscopy_compare_param_to_bak() )
    {
        return 1;
    }


//    msgBox->setTipInfo(tr("是否保存？"));
//    msgBox->setBottonText("OK", tr("是"));
//    msgBox->showBotton("Cancel");
//    msgBox->setBottonText("Cancel", tr("否"));
//    msgBox->showKMessageBox();
   // if( msgBox->getStates() == KMessageBox::OK)
    {

        //microscopy_insert_delete_Db();
       // main_control_data.microParam.clear();


        for( i = 0; i < paramMicroDataBak.count(); i++ )
        {
          //  main_control_data.microParam.append( paramMicroDataBak[i] );
        }

        //microscopy_update_Db();
        //system("sync");
       // for(i=0;i<logtemp.count();i++)
         // save_journal_info(tr("修改镜检参数设置"),logtemp.at(i),0);
    }

    return 1;
}


void ParamMicroscopyView::on_Btn_new_clicked()
{
    int rowNum = pModel->rowCount();

    if(rowNum == 0)
    {
        pModel->insertRow(rowNum);
        pModel->setData(pModel->index(rowNum, 0), "");

        // 直接提交
        pModel->submitAll();
        return;
    }

    if( rowNum >= 40 )
    {
//        msgBox->setTipInfo(tr("记录已满40条!"));
//        msgBox->hideBotton("Cancel");
//        msgBox->setBottonText("OK", tr("确定"));
//        msgBox->showKMessageBox();
//        msgBox->showBotton("Cancel");
//        msgBox->setBottonText("OK", tr("是"));
        return;
    }

    rowNum--;
    if(pModel->data(pModel->index(rowNum, 0)).toString().trimmed() == "")
    {
        return;
    }
    rowNum++;

    pModel->insertRow(rowNum);
    pModel->setData(pModel->index(rowNum, 0), "");

    // 直接提交
    pModel->submitAll();
}

void ParamMicroscopyView::on_Btn_del_clicked()
{

    int row_count = 0;
    int selectRow = 0;

//    msgBox->setTipInfo(tr("是否确认删除？"));
//    msgBox->setBottonText("OK", tr("是"));
//    msgBox->showBotton("Cancel");
//    msgBox->setBottonText("Cancel", tr("否"));
//    msgBox->showKMessageBox();
//    if( msgBox->getStates() == KMessageBox::Cancel)
//    {
//        return;
//    }


    row_count = pModel->rowCount();
    if( row_count <= 1 )
    {
        ui->Btn_del->setEnabled(false);
    }

   int curRow = pMicroscopyTbv->getTableView()->currentIndex().row();
   pModel->removeRow(curRow);
   pModel->submitAll();
}

void ParamMicroscopyView::myClearFocus()
{

}
